/**
 * 
 */
package com.wrsr.examsys.student.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.wrsr.examsys.domain.Student;
import com.wrsr.examsys.student.service.StudentService;
import com.wrsr.examsys.util.MD5;
import com.wrsr.examsys.util.MyEmail;
import com.wrsr.examsys.util.MyRandom;
import com.wrsr.examsys.valited.StringIsValitedUtils;

/**
 * @author <a href="mailto:2397431783@qq.com">wangruishun</a>
 *
 *         2014年12月26日 下午4:19:07
 */

@Controller("studentAction")
@Scope("prototype")
public class StudentAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware {

	private static final long serialVersionUID = 2991448180749464623L;
	private String msg;
	private Student student;
	private String email;
	private String userName;
	private String flag = "0";
	private String pwd;
	private String repwd;
	private String code;

	@Resource
	private StudentService studentService;
	private HttpServletRequest request;
	private HttpServletResponse response;

	public String exitStudent() {
		request.getSession().setAttribute("student", "");
		request.getSession().removeAttribute("student");
		
		return SUCCESS;
	}

	public String studentLogin() {
		return SUCCESS;
	}

	public String studentSave() {
		student = new Student();
		if (email == "" || pwd == "") {
			msg = "*亲，您输入的email或密码不正确！";
			return "wrong";

		} else {
			student.setEmail(email);
			student.setPwd(MD5.GetMD5Code(pwd));
			if (studentService.getStudentByEmailAndPwd(student)) {
				student = studentService.getEntity(email);
				request.getSession().setAttribute("student", student);
				return SUCCESS;
			} else {
				msg = "*亲，您输入的email或密码不正确！";
				return "wrong";
			}
		}
	}

	public String valitedEmail() {
		if (!studentService.emailIsValited(email)) {
			msg = "*亲爱的,您输入不是email格式哦！";
			flag = "0";
		} else if (studentService.emailIsExist(email)) {
			msg = "*亲爱的,您的email在我心中不是独一无二的呀！";
			flag = "0";
		} else {
			msg = "恭喜亲,我可以接受您的email！";
			flag = "1";
		}
		return SUCCESS;
	}

	public String valitedName() {
		if (!StringIsValitedUtils.nameIsValited(userName)) {
			msg = "*亲爱的，请输入您自己的中文真实姓名！";
			flag = "0";
		} else {
			flag = "1";
			msg = "恭喜亲，您的名字合法！";
		}
		return SUCCESS;
	}

	public String valitedPwd() {
		if (!StringIsValitedUtils.pwdIsValited(pwd)) {
			msg = "*宝贝，我们的密码规则以字母开头，长度在6~18之间，只能包含字符、数字和下划线哦";
			flag = "0";
		} else {
			flag = "1";
			msg = "恭喜亲，您的密码符合规则！";
		}
		return SUCCESS;
	}

	public String valitedRepwd() {
		if (repwd.equals(pwd) && StringIsValitedUtils.pwdIsValited(repwd)) {
			flag = "1";
			msg = "真棒，两次密码一样一样的！";
		} else {
			msg = "*笨蛋，您输入的密码和上次不一样，是不可能通过的！";
			flag = "0";
		}
		return SUCCESS;
	}

	public String valitedCode() {

		if (studentService.codeIsRight(code, request)) {
			flag = "1";
			msg = "恭喜您，输入非常精确";
		} else {
			msg = "*真遗憾，输入的验证码不正确哦！";
			flag = "0";
		}
		return SUCCESS;
	}

	@SuppressWarnings("static-access")
	public String studentRegist() {
		student = new Student();
		student.setEmail(email);
		student.setName(userName);
		student.setPwd(MD5.GetMD5Code(pwd));
		MyEmail myEmail = new MyEmail(false);
		String cd = new MyRandom().getCode();
		request.getSession().setAttribute("code", cd);
		myEmail.doSendHtmlEmail("青岛农业大学精品课程网注册激活码，请勿回复此邮件", "激活码：" + cd, email);
		return SUCCESS;
	}

	public String saveStudent() {
		String cd = (String) request.getSession().getAttribute("code");
		response.setHeader("progma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		if (cd.equals(code) || cd == code) {
			studentService.saveEntity(student);
			return SUCCESS;
		} else {
			msg = "*亲，验证码不正确，请到邮箱核实！";
			return "wrong";
		}
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the student
	 */
	public Student getStudent() {
		return student;
	}

	/**
	 * @param student
	 *            the student to set
	 */
	public void setStudent(Student student) {
		this.student = student;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return userName;
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(String userName) {
		this.userName = userName;
	}

	/**
	 * @return the flag
	 */
	public String getFlag() {
		return flag;
	}

	/**
	 * @param flag
	 *            the flag to set
	 */
	public void setFlag(String flag) {
		this.flag = flag;
	}

	/**
	 * @return the pwd
	 */
	public String getPwd() {
		return pwd;
	}

	/**
	 * @param pwd
	 *            the pwd to set
	 */
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	/**
	 * @return the repwd
	 */
	public String getRepwd() {
		return repwd;
	}

	/**
	 * @param repwd
	 *            the repwd to set
	 */
	public void setRepwd(String repwd) {
		this.repwd = repwd;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(
	 * javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.struts2.interceptor.ServletResponseAware#setServletResponse
	 * (javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;

	}

}
